package com.habdqn.jspdemo.im.servlet.company;

import com.habdqn.jspdemo.im.service.CompanyService;
import com.habdqn.jspdemo.im.service.impl.CompanyServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "DeleteCompanyServlet",value = "/company/delete")
public class DeleteCompanyServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String strId = request.getParameter("id");
        CompanyService companyService = new CompanyServiceImpl();
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        if(companyService.deleteById(Integer.parseInt(strId))){
            out.print("<script>");
            out.print("alert('删除成功！');");
            out.print("location.href='/company/list';");
            out.print("</script>");
        }else{
            out.print("<script>");
            out.print("alert('删除失败!');");
            out.print("history.back();");
            out.print("</script>");
        }
    }
}
